
<h1><span class="yiyi-st" id="yiyi-12">numpy.prod</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.prod.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.prod.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.prod"><span class="yiyi-st" id="yiyi-13"> <code class="descclassname">numpy.</code><code class="descname">prod</code><span class="sig-paren">(</span><em>a</em>, <em>axis=None</em>, <em>dtype=None</em>, <em>out=None</em>, <em>keepdims=&lt;class numpy._globals._NoValue&gt;</em><span class="sig-paren">)</span><a class="reference external" href="http://github.com/numpy/numpy/blob/v1.11.3/numpy/core/fromnumeric.py#L2433-L2543"><span class="viewcode-link">[source]</span></a></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-14">返回给定轴上的数组元素的乘积。</span></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name">
<col class="field-body">
<tbody valign="top">
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-15">参数：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-16"><strong>a</strong>：array_like</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-17">输入数据。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-18"><strong>axis</strong>：无或int或tuple ints，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-19">执行产品的轴或轴。</span><span class="yiyi-st" id="yiyi-20">默认值axis = None将计算输入数组中所有元素的乘积。</span><span class="yiyi-st" id="yiyi-21">如果轴为负，则从最后一个轴计数到第一个轴。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-22"><span class="versionmodified">版本1.7.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-23">如果axis是ints的元组，那么将对元组中指定的所有轴执行乘积，而不是像以前一样对单个轴或所有轴执行乘积。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-24"><strong>dtype</strong>：dtype，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-25">返回的数组的类型，以及与元素相乘的累加器的类型。</span><span class="yiyi-st" id="yiyi-26">除非<em class="xref py py-obj">a</em>具有精度低于默认平台整数的整数dtype，因此默认使用<em class="xref py py-obj">a</em>的dtype。</span><span class="yiyi-st" id="yiyi-27">在这种情况下，如果<em class="xref py py-obj">a</em>被签名，则使用平台整数，而如果<em class="xref py py-obj">a</em>是无符号的，则使用与平台整数相同精度的无符号整数。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-28"><strong>out</strong>：ndarray，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-29">用于放置结果的替代输出数组。</span><span class="yiyi-st" id="yiyi-30">它必须具有与预期输出相同的形状，但如果必要，将输出输出值的类型。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-31"><strong>keepdims</strong>：bool，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-32">如果设置为True，则缩小的轴在结果中保留为尺寸为1的尺寸。</span><span class="yiyi-st" id="yiyi-33">使用此选项，结果将根据输入数组正确地广播。</span></p>
<p><span class="yiyi-st" id="yiyi-34">如果传递默认值，则<em class="xref py py-obj">keepdims</em>将不会传递到<a class="reference internal" href="numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-obj docutils literal"><span class="pre">ndarray</span></code></a>的子类的<a class="reference internal" href="#numpy.prod" title="numpy.prod"><code class="xref py py-obj docutils literal"><span class="pre">prod</span></code></a>方法，默认值为。</span><span class="yiyi-st" id="yiyi-35">如果子类<a class="reference internal" href="numpy.sum.html#numpy.sum" title="numpy.sum"><code class="xref py py-obj docutils literal"><span class="pre">sum</span></code></a>方法不实现<em class="xref py py-obj">keepdims</em>，则会引发任何异常。</span></p>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name"><span class="yiyi-st" id="yiyi-36">返回：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-37"><strong>product_along_axis</strong>：ndarray，请参阅上面的<a class="reference internal" href="numpy.dtype.html#numpy.dtype" title="numpy.dtype"><code class="xref py py-obj docutils literal"><span class="pre">dtype</span></code></a>参数。</span></p>
<blockquote class="last">
<div><p><span class="yiyi-st" id="yiyi-38">形状为<em class="xref py py-obj">a</em>但删除指定轴的数组。</span><span class="yiyi-st" id="yiyi-39">如果指定，返回<em class="xref py py-obj">out</em>的引用。</span></p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-40">也可以看看</span></p>
<dl class="last docutils">
<dt><span class="yiyi-st" id="yiyi-41"><a class="reference internal" href="numpy.ndarray.prod.html#numpy.ndarray.prod" title="numpy.ndarray.prod"><code class="xref py py-obj docutils literal"><span class="pre">ndarray.prod</span></code></a></span></dt>
<dd><span class="yiyi-st" id="yiyi-42">等效法</span></dd>
<dt><span class="yiyi-st" id="yiyi-43"><code class="xref py py-obj docutils literal"><span class="pre">numpy.doc.ufuncs</span></code></span></dt>
<dd><span class="yiyi-st" id="yiyi-44">节“输出参数”</span></dd>
</dl>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-45">笔记</span></p>
<p><span class="yiyi-st" id="yiyi-46">当使用整数类型时，算术是模块化的，并且在溢出时不产生错误。</span><span class="yiyi-st" id="yiyi-47">这意味着，在32位平台上：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">536870910</span><span class="p">,</span> <span class="mi">536870910</span><span class="p">,</span> <span class="mi">536870910</span><span class="p">,</span> <span class="mi">536870910</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">prod</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="c1">#random</span>
<span class="go">16</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-48">空数组的乘积是中性元素1：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">prod</span><span class="p">([])</span>
<span class="go">1.0</span>
</pre></div>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-49">例子</span></p>
<p><span class="yiyi-st" id="yiyi-50">默认情况下，计算所有元素的乘积：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">prod</span><span class="p">([</span><span class="mf">1.</span><span class="p">,</span><span class="mf">2.</span><span class="p">])</span>
<span class="go">2.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-51">即使输入数组是二维的：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">prod</span><span class="p">([[</span><span class="mf">1.</span><span class="p">,</span><span class="mf">2.</span><span class="p">],[</span><span class="mf">3.</span><span class="p">,</span><span class="mf">4.</span><span class="p">]])</span>
<span class="go">24.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-52">但是我们也可以指定要乘以的轴：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">prod</span><span class="p">([[</span><span class="mf">1.</span><span class="p">,</span><span class="mf">2.</span><span class="p">],[</span><span class="mf">3.</span><span class="p">,</span><span class="mf">4.</span><span class="p">]],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="go">array([  2.,  12.])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-53">如果<em class="xref py py-obj">x</em>的类型为无符号，则输出类型为无符号平台整数：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">uint8</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">prod</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">dtype</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">uint</span>
<span class="go">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-54">如果<em class="xref py py-obj">x</em>是有符号整数类型，则输出类型是默认平台整数：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int8</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">prod</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">dtype</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">int</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>
